package com.canin.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.canin.Constant;
import com.canin.dao.BreedDAO;
import com.canin.dao.DAOFactory;
import com.canin.model.Breed;
import com.canin.model.BreedDisplay;
import com.canin.util.CaninUtil;
import com.opensymphony.xwork2.ActionSupport;

public class Breed2Action extends ActionSupport  implements SessionAware{

	private static final long serialVersionUID = 1954591024241616596L;
	
	private Map<String, Object> userSession;
	private Breed breedForm = new Breed();
	private Breed breed = new Breed();
	
	private Map<String, String> sizes = null;
	private Map<String, String> neckShapes = null;
	private Map<String, String> faceShapes = null;
	private Map<String, String> nameList = null;

	private List<Breed> breedList = new ArrayList<Breed>();
	private List<BreedDisplay> breedDisplayList = new ArrayList<BreedDisplay>();
	
	public String main()
	{
		return "main";		
	}
	
	public String toAdd()
	{
		initialValue();
		
		return "add";
	}
	
	public String add() throws Exception
	{
		initialValue();
		
		if ( breedForm != null )
		{
			// Required Fields
			if ( !"".equals( breedForm.getBreedName() )
				&& !"".equals( breedForm.getMinWeight() )
				&& !"".equals( breedForm.getMaxWeight() )
				&& !"-1".equals( breedForm.getSize() )
				&& !"".equals( breedForm.getAdultMinAgeYear() )
				&& !"".equals( breedForm.getAdultMinAgeMonth() )
				&& !"".equals( breedForm.getSeniorMinAgeYear() )
				&& !"".equals( breedForm.getSeniorMinAgeMonth() )
				&& !"-1".equals( breedForm.getNeckShape() )
				&& !"-1".equals( breedForm.getFaceShape() ) )
			{
				DAOFactory daoFactory = new DAOFactory();
				daoFactory.beginConnection();

				BreedDAO breedDAO = daoFactory.createBreedDAO();
				breedDAO.createNewBreed( breedForm );
				
				addActionMessage("เพิ่มข้อมูล " + breedForm.getBreedName() + " เรียบร้อย");
			}
			else
			{
				addActionMessage("กรุณากรอกข้อมูลให้ครบถ้วน");
			}
		}
		
		return "add";		
	}
	
	public String get() throws Exception
	{
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();

		BreedDAO breedDAO = daoFactory.createBreedDAO();
		breedList = breedDAO.getAllBreed();
		breedDisplayList = convertBreedToBreedDisplay(breedList);
		
		return "get";		
	}

	private List<BreedDisplay> convertBreedToBreedDisplay( List<Breed> breedList )
	{
		breedDisplayList = new ArrayList<BreedDisplay>();
		BreedDisplay breedDisplay ;
		
		initialValue();
		
		for ( Breed breed : breedList )
		{
			breedDisplay = new BreedDisplay();
			breedDisplay.setBreedId(breed.getBreedId());
			breedDisplay.setBreedName(breed.getBreedName());
			breedDisplay.setWeight( breed.getMinWeight() + "-" + breed.getMaxWeight() );
			breedDisplay.setSize(sizes.get( breed.getSize() ));
//			breedDisplay.setPuppy( "แรกเกิด - " breed.get);
			
			String adult = "";
			if ( breed.getAdultMinAgeYear() != 0 )
			{
				adult = breed.getAdultMinAgeYear() + "ปี  ";
			}
			if ( breed.getAdultMinAgeMonth() != 0 )
			{
				adult = adult + breed.getAdultMinAgeMonth() + "เดือน";
			}
			breedDisplay.setAdult(adult);
			
			String senior = "";
			if ( breed.getAdultMinAgeYear() != 0 )
			{
				senior = breed.getSeniorMinAgeYear() + "ปี  ";
			}
			if ( breed.getAdultMinAgeMonth() != 0 )
			{
				senior = senior + breed.getSeniorMinAgeMonth() + "เดือน";
			}
			breedDisplay.setSenior(senior);
			
			breedDisplay.setNeck(neckShapes.get(breed.getNeckShape()));
			breedDisplay.setFace(faceShapes.get(breed.getFaceShape()));
		
			breedDisplayList.add(breedDisplay);
		}
		
		return breedDisplayList;
	}
	
	public String toEdit() throws Exception
	{
		setNameList( CaninUtil.retrieveBreedNameList() );
		initialValue();
		
		return "edit";		
	}
	
	public String search() throws Exception
	{
		setNameList( CaninUtil.retrieveBreedNameList() );
		initialValue();
//		System.out.println( "\n\n ==>" + breedForm.getNameList() + "<== \n\n" );
		
		// search for breed --> add to breedForm
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		BreedDAO breedDAO = daoFactory.createBreedDAO();
		breed = breedDAO.getBreedById( Integer.parseInt(breedForm.getNameList()) );
		
		breedForm.setBreedId(breed.getBreedId());
		breedForm.setBreedName(breed.getBreedName());
		breedForm.setSize(breed.getSize());
		breedForm.setMinWeight(breed.getMinWeight());
		breedForm.setMaxWeight(breed.getMaxWeight());
		breedForm.setAdultMinAgeYear(breed.getAdultMinAgeYear());
		breedForm.setAdultMinAgeMonth(breed.getAdultMinAgeMonth());
		breedForm.setSeniorMinAgeYear(breed.getSeniorMinAgeYear());
		breedForm.setSeniorMinAgeMonth(breed.getSeniorMinAgeMonth());
		breedForm.setNeckShape(breed.getNeckShape());
		breedForm.setFaceShape(breed.getFaceShape());
		
		System.out.println( "\n\n ==>" + breed.getSize() + ":" + breed.getNeckShape() + ":" + breed.getFaceShape() + "<== \n\n" );
		userSession.put(Constant.BREED, breed);
		
		return "search";
	}
	
	public String edit() throws Exception
	{
		boolean result = false;
		
		breed = (Breed)userSession.get( Constant.BREED );
		
		setNameList( CaninUtil.retrieveBreedNameList() );
		initialValue();
		
		System.out.println( "\n\n ==>" + breed.getBreedId() + "<== \n\n" );
		breedForm.setBreedId(breed.getBreedId());
		
		// Update Breed
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		BreedDAO breedDAO = daoFactory.createBreedDAO();
		result = breedDAO.updateBreed(breedForm);
		
		if ( result )
		{
			addActionMessage("แก้ไขข้อมูลเรียบร้อย");
			setNameList( CaninUtil.retrieveBreedNameList() );
		}
		
		return "edit";		
	}
	
	private void initialValue()
	{
		sizes = CaninUtil.initializeSizes();
		neckShapes = initializeNeckShapes();
		faceShapes = initializeFaceShapes();
	}
	
	private Map<String, String> initializeNeckShapes()
	{
		HashMap<String, String> neckShapes = new HashMap<>();
		neckShapes.put(Breed.NECK_SHAPE_NORMAL, getText("คอปกติ"));
		neckShapes.put(Breed.NECK_SHAPE_SLENDER_SMALLHEAD, getText("คอเรียวยาว ส่วนหัวเล็กกว่าลำคอ"));
		neckShapes.put(Breed.NECK_SHAPE_SHORT, getText("คอสั้น"));
		neckShapes.put(Breed.NECK_SHAPE_SLENDER, getText("คอเรียวยาว"));
		neckShapes.put(Breed.NECK_SHAPE_SMALL, getText("คอเล็ก"));
		
		return neckShapes;
	}
	
	private Map<String, String> initializeFaceShapes()
	{
		HashMap<String, String> faceShapes = new HashMap<>();
		faceShapes.put(Breed.FACE_SHAPE_LONG, getText("ยาว"));
		faceShapes.put(Breed.FACE_SHAPE_SMALL, getText("เล็ก"));
		faceShapes.put(Breed.FACE_SHAPE_SHORT, getText("สั้น"));
		
		return faceShapes;
	}
	
	@Override
	public void setSession(Map<String, Object> userSession)
	{
		this.userSession = userSession;
	}

	public Breed getBreedForm()
	{
		return breedForm;
	}

	public void setBreedForm(Breed breedForm)
	{
		this.breedForm = breedForm;
	}

	public Map<String, String> getSizes()
	{
		return sizes;
	}

	public void setSizes(Map<String, String> sizes)
	{
		this.sizes = sizes;
	}

	public Map<String, String> getNeckShapes()
	{
		return neckShapes;
	}

	public void setNeckShapes(Map<String, String> neckShapes)
	{
		this.neckShapes = neckShapes;
	}

	public Map<String, String> getFaceShapes()
	{
		return faceShapes;
	}

	public void setFaceShapes(Map<String, String> faceShapes)
	{
		this.faceShapes = faceShapes;
	}

	public List<Breed> getBreedList()
	{
		return breedList;
	}

	public void setBreedList(List<Breed> breedList)
	{
		this.breedList = breedList;
	}

	public Map<String, String> getNameList()
	{
		return nameList;
	}

	public void setNameList(Map<String, String> nameList)
	{
		this.nameList = nameList;
	}
	
	public Breed getBreed()
	{
		return breed;
	}

	public void setBreed(Breed breed)
	{
		this.breed = breed;
	}
	
	public List<BreedDisplay> getBreedDisplayList()
	{
		return breedDisplayList;
	}

	public void setBreedDisplayList(List<BreedDisplay> breedDisplayList)
	{
		this.breedDisplayList = breedDisplayList;
	}

}
